package cn.dnui_zjq823.dnuifood_16110100823.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_zjq823.dnuifood_16110100823.R;
import cn.dnui_zjq823.dnuifood_16110100823.bean.FoodBean;
import cn.dnui_zjq823.dnuifood_16110100823.detail.FoodDetail;
import cn.dnui_zjq823.dnuifood_16110100823.detail.ShopDetail;

/**
 * Created by dell on 2018/11/8.
 */

public class FoodAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<FoodBean> list;
    private Context context;
    private String url="http://172.24.10.175:8080/foodService/";

    public FoodAdapter(Context context) {
        this.context = context;
    }

    public void setList(List<FoodBean> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.food_item,parent,false);
        return new FoodAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final FoodBean foodBean=list.get(position);
        if (foodBean==null)
            return;
        FoodAdapter.ViewHolder viewHolder=(FoodAdapter.ViewHolder)holder;
        Picasso.with(context).load(url+foodBean.getPic()).fit().placeholder(R.mipmap.ic_launcher).into(viewHolder.food_pic);
        viewHolder.tv_foodname.setText(foodBean.getFoodname());
        viewHolder.tv_foodprice.setText(String.valueOf(foodBean.getPrice())+"元");
        viewHolder.tv_foodintro.setText(foodBean.getIntro());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=String.valueOf(list.get(position).getFood_id());
                String str1=String.valueOf(list.get(position).getFoodname());
                String str2=String.valueOf(list.get(position).getPrice());
                String str3=String.valueOf(list.get(position).getIntro());
                String str4=String.valueOf(list.get(position).getRecommand());
                String str5=String.valueOf(list.get(position).getPic());
                Toast.makeText(context,"id"+str, Toast.LENGTH_SHORT).show();
                Intent intent= new Intent(context, FoodDetail.class);
                intent.putExtra("foodid",str);
                intent.putExtra("foodname",str1);
                intent.putExtra("foodprice",str2);
                intent.putExtra("foodintro",str3);
                intent.putExtra("foodrecommand",str4);
                intent.putExtra("foodpic",str5);
                context.startActivity(intent);

            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_foodname,tv_foodprice,tv_foodintro;
        ImageView food_pic;
        public ViewHolder(View itemView) {
            super(itemView);
            food_pic=(ImageView) itemView.findViewById(R.id.food_pic);
            tv_foodname=(TextView)itemView.findViewById(R.id.tv_foodname);
            tv_foodprice=(TextView)itemView.findViewById(R.id.tv_foodprice);
            tv_foodintro=(TextView)itemView.findViewById(R.id.tv_foodintro);

        }
    }
}
